"""
DOCUMENT ME!
"""

from board import * # Board, BLACK, WHITE, MARKER_*

class Cursor(object):
    def __init__(self, game):
        self.game = game
        self.node = None # OR: assign game.head here?  (will it have a head here?)

        try:
            size = int(self.game.head.properties['SZ'])
        except:
            size = 19 # BUT: what if the game has no nodes yet?
        self.board = Board(size)

        # set up handicap
        if 'AB' in game.head.properties:
            self.board.set_handicap(game.head.properties['AB'])
        # BUT: what if the handicap hasn't been set yet?

    def move_to(self, node):
        self.node = node

        # play the game, up thru this point
        # (FUTURE/PERF: if it's close to the old value, do this much more efficiently.)

        # make the list of nodes that i'll replay
        path = []
        ptr = node
        while ptr:
            path += [ ptr ]
            ptr = ptr.parent
        del ptr
        path.reverse()

        # replay them
        self.board.reset()
        for ptr in path:
            # add non-played stones -- FIXME: need an add-stone method?
            if 'AB' in ptr.properties:
                for pt in ptr.properties['AB']:
                    self.board.play_stone(pt, BLACK)
            if 'AW' in ptr.properties:
                for pt in ptr.properties['AW']:
                    self.board.play_stone(pt, WHITE)

            # erase stones
            if 'AE' in ptr.properties:
                for pt in ptr.properties['AE']:
                    self.board.erase(pt)

            # add played stone
            if 'B' in ptr.properties:
                if ptr.properties['B']:
                    self.board.play_stone(ptr.properties['B'], BLACK)
                else:
                    self.board.play_pass()
            if 'W' in ptr.properties:
                if ptr.properties['W']:
                    self.board.play_stone(ptr.properties['W'], WHITE)
                else:
                    self.board.play_pass()

        # set markers: squares (SQ), triangles (TR), x (MA?), circles (CR), labels (LB:xxx)
        ptr = node
        if 'SQ' in ptr.properties:
            for s in ptr.properties['SQ']:
                self.board.mark(s, MARKER_SQUARE)
        if 'TR' in ptr.properties:
            for s in ptr.properties['TR']:
                self.board.mark(s, MARKER_TRIANGLE)
        if 'CR' in ptr.properties:
            for s in ptr.properties['CR']:
                self.board.mark(s, MARKER_CIRCLE)
        if 'MA' in ptr.properties:
            for s in ptr.properties['MA']:
                self.board.mark(s, MARKER_X)
        if 'TW' in ptr.properties:
            for s in ptr.properties['TW']:
                self.board.mark(s, MARKER_WHITE_TERRITORY)
        if 'TB' in ptr.properties:
            for s in ptr.properties['TB']:
                self.board.mark(s, MARKER_BLACK_TERRITORY)
        if 'LB' in ptr.properties:
            for pt,label in ptr.properties['LB'].items():
                self.board.mark(pt, label)
        if 'SL' in ptr.properties:
            for s in ptr.properties['SL']:
                self.board.mark(s, MARKER_SELECTED)

        # these markers are old!  (FF[1]?)
        if 'L' in ptr.properties:
            # start labelling at 'A'; ASSUME that there's no more than 26 points -- CHECK: what does FF[4] say?
            for i,point in enumerate(ptr.properties['L']):
                self.board.mark(point, chr(ord('A') + i))
        if 'M' in ptr.properties:
            for s in ptr.properties['M']:
                self.board.mark(s, MARKER_TRIANGLE) # (cgoban uses triangles, so i will, too)


    ## BACKWARDS COMPATIBILITY: use self.board, for now
    # -- for scores, as well
